package com.ethank.controller.ocr;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ethank.common.core.domain.AjaxResult;
import com.ethank.common.core.text.Convert;
import com.ethank.service.OcrService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author Administrator
 * @date 2024/2/22
 * @Description 票据识别
 */
@RequestMapping("/ocr")
@RestController
public class OcrController {

    @Resource
    private OcrService ocrService;


    /**
     * @Description: 上传票据-超级BOSS
     * @Param: [file]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Date: 2024-02-23
     */
    @PostMapping("/billUpload")
    public AjaxResult billUpload(MultipartFile[] files, @RequestParam(name = "type", required = false, defaultValue = "0"
    ) Integer type,Integer userId) throws Exception {
        return ocrService.billUpload(files, type,userId);
    }

    /**
     * @Description: 发票核验
     * @Param: [invoiceNo, invoiceDate]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Author: tf
     * @Date: 2024-02-26
     */
    @PostMapping("/verifyInvoice")
    public AjaxResult verifyVATInvoice(String invoiceCode,String invoiceNo, String invoiceDate,String verifyCode) {
        return ocrService.verifyInvoice(invoiceCode,invoiceNo, invoiceDate,verifyCode);
    }

    /**
     * @Description: 保存发票数据
     * @Param: [jsonObject]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Date: 2024-03-05
     */
    @PostMapping("/addTicket")
    public AjaxResult addTicket(@RequestBody JSONObject jsonObject) {

        return ocrService.addTicket(jsonObject);
    }

    /**
     * 获取所有票据识别枚举
     *
     * @return 数据
     */
    @PostMapping(value = "/getBillLists")
    public AjaxResult getBillLists() {
        return ocrService.getBillLists();
    }



    /**
     * @Description: 企业微信拍照或相册识别票据
     * @Param: [type, serverIds]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Author: tf
     * @Date: 2024-03-07
     */
    @PostMapping("/wx/media")
    public AjaxResult getWxMediaTicket(@RequestParam(name = "type", required = false, defaultValue = "1"
    ) Integer type, String[] mediaIds,Integer userId) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, FileNotFoundException, ClassNotFoundException {
        return ocrService.recognitionTicket(mediaIds, type,userId);
    }


    /**
     * 获取票据列表及历史
     *
     * @return 数据信息
     */
    @PostMapping("getListRecord")
    public AjaxResult getListRecord(@RequestParam(name="page",required = false,defaultValue = "1") Integer page,@RequestParam(name="size",required = false,defaultValue = "10") Integer size,@RequestParam(name="beginTime",required = false) String beginTime,@RequestParam(name="finishTime",required = false) String finishTime,@RequestParam(name="createTime",required = false) String createTime,@RequestParam(name="endTime",required = false) String endTime ,@RequestParam(name="type",required = false) String type,Integer userId,@RequestParam(name="state",required = false,defaultValue = "") Integer status) {
        return ocrService.getListRecord(userId,page,size,beginTime,finishTime,createTime,endTime,type,status);
    }


    /**
    * @Description: 查询发票详情
    * @Param: [jsonObject]
    * @return: com.ethank.common.core.domain.AjaxResult
    * @Author: tf
    * @Date: 2024-03-13
    */
    @PostMapping("/getInvoiceInfo")
    public AjaxResult getInvoiceInfo(String userId,Integer id,String invoiceNumber,@RequestParam(name="invoiceCode",required = false) String invoiceCode ){
        return ocrService.getInvoiceInfo(userId,id,invoiceNumber,invoiceCode);
    }

    /**
     * @Description: 查询发票图片
     * @Param: [jsonObject]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Author: tf
     * @Date: 2024-03-13
     */
    @PostMapping("/getInvoiceImage")
    public AjaxResult getInvoiceImage(String userId,String invoiceNumber,@RequestParam(name="invoiceCode",required = false) String invoiceCode ){
        return ocrService.getInvoiceImage(userId,invoiceNumber,invoiceCode);
    }
    /**
    * @Description: 查询发票抬头
    * @Param: []
    * @return: com.ethank.common.core.domain.AjaxResult
    * @Author: tf
    * @Date: 2024-04-02
    */
    @PostMapping("/getListInvoiceHeader")
    public AjaxResult getInvoiceHeader(@RequestBody JSONObject jsonObject){
         Integer page = Convert.toInt(jsonObject.getOrDefault("page",1));
         Integer size = Convert.toInt(jsonObject.getOrDefault("size",100));
         String searchKey = Convert.toStr(jsonObject.getOrDefault("searchKey",""));
        return ocrService.getInvoiceHeader(page,size,searchKey);
    }


    /**
     * 删除发票
     *
     * @param map 发票id
     * @return 返回
     */
    @PostMapping("/deleteInvoiceInfo")
    public AjaxResult deleteInvoiceInfo(@RequestBody Map<String,Object> map) {
        return ocrService.deleteInvoiceInfo(map);
    }

    /**
     * 点击编辑按钮时，验证发票是否在流程中或者已核销的状态
     *
     * @param id 发票信息
     * @return 返回
     */
    @GetMapping("/editCheckInvoiceIsUse")
    public AjaxResult editCheckInvoiceIsUse(@RequestParam String id) {
        return ocrService.editCheckInvoiceIsUse(id);
    }
    /**
    * @Description: 查询门店欠票列表-超级BOSS提现时查询欠票
    * @Param: [shopId]
    * @return: com.ethank.common.core.domain.AjaxResult
    * @Author: tf
    * @Date: 2024-10-25
    */
    @PostMapping("/getShopInvoiceList")
    public AjaxResult getShopInvoiceList(@RequestBody JSONObject jsonObject){
        String shopId = Convert.toStr(jsonObject.getString("shopId"),"");
        String sign = Convert.toStr(jsonObject.getString("sign"),"");
        return ocrService.getShopInvoiceList(shopId,sign);
    }
    /**
     * @Description: 提交门店发票信息-超级BOSS提现时补发票信息
     * @Param: [shopId]
     * @return: com.ethank.common.core.domain.AjaxResult
     * @Author: tf
     * @Date: 2024-10-25
     */

    @PostMapping("/saveShopInvoice")
    public  AjaxResult saveShopInvoice(@RequestBody JSONObject jsonObject){
        return ocrService.saveShopInvoice(jsonObject);
    }

    /**
    * @Description: 检测发票号是否已使用-超级BOSS提现时查询欠票是否使用
    * @Param: [jsonObject]
    * @return: com.ethank.common.core.domain.AjaxResult
    * @Author: tf
    * @Date: 2024-10-28
    */
    @PostMapping("/checkShopInvoice")
    public  AjaxResult checkShopInvoice(@RequestBody JSONObject jsonObject){
        return ocrService.checkShopInvoice(jsonObject);
    }



}
